package com.vueapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vueapi.domain.HjWebsite;
import com.vueapi.domain.vo.DataCountVO;
import com.vueapi.domain.vo.SevenDayDataCountVO;
import com.vueapi.service.*;
import com.vueapi.utils.DateUtils;
import com.vueapi.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Classname AdminIndexController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/23 10:37
 * @Version 1.0.0
 */
@Service
public class AdminIndexServiceImpl implements AdminIndexService {

    @Resource
    private HjArticleService articleService;

    @Resource
    private HjWebsiteService websiteService;

    @Resource
    private HjCommentService commentService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private HjTagService tagService;

    @Resource
    private VisitLogService visitLogService;

    @Override
    public ResultUtils getDataCount() {

        // 获取文章总数
        Integer articleCount = articleService.getCount();

        // 获取网站访问量
        Integer websiteViewCount = websiteService.getOne(new LambdaQueryWrapper<HjWebsite>().last("limit 1")).getViewCount();

        // 获取评论总数
        Integer commentCount = commentService.getCount();

        // 获取留言总数
        Integer messageCount = 888;

        DataCountVO dataCountVO = new DataCountVO();
        dataCountVO.setArticleCount(articleCount);
        dataCountVO.setWebsiteViewCount(websiteViewCount);
        dataCountVO.setCommentCount(commentCount);
        dataCountVO.setMessageCount(messageCount);

        return ResultUtils.success(dataCountVO);
    }

    @Override
    public ResultUtils getSevenDayData() {
        String sevenDayData = redisTemplate.opsForValue().get("sevenDayData");
        if(sevenDayData == null){
            List<Integer> blogAddCountList = articleService.getBlogAddCount();
            List<Integer> commentAddCountList = commentService.getCommentAddCount();
            List<Integer> tagAddCountList = tagService.getTagAddCount();
            List<Integer> visitAddCount = visitLogService.getVisitAddCount();

            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            ArrayList<String> sevenDayNameList = new ArrayList<>();

            for (int i = 7; i >= 1; i--) {
                Date today = DateUtils.calLastDay(i);
                sevenDayNameList.add(sdf.format(today));
            }

            SevenDayDataCountVO sevenDayDataCountVO = new SevenDayDataCountVO();
            sevenDayDataCountVO.setSevenDayName(sevenDayNameList);
            sevenDayDataCountVO.setBlogAddCount(blogAddCountList);
            sevenDayDataCountVO.setCommentAddCount(commentAddCountList);
            sevenDayDataCountVO.setMessageAddCount(commentAddCountList);
            sevenDayDataCountVO.setTagAddCount(tagAddCountList);
            sevenDayDataCountVO.setVisitAddCount(visitAddCount);

            redisTemplate.opsForValue().set("sevenDayData", JSON.toJSONString(sevenDayDataCountVO),1, TimeUnit.DAYS);
            return ResultUtils.success(sevenDayDataCountVO);
        }
        SevenDayDataCountVO sevenDayDataCountVO = JSON.parseObject(sevenDayData, SevenDayDataCountVO.class);
        return ResultUtils.success(sevenDayDataCountVO);
    }
}
